class Utils {

	/********************************* ajax请求 ***********************************/

	static ajax(config: {
		url: string,
		method?: string,
		params?: Object
	}, callback?: Function): void {
		let xmlhttp: XMLHttpRequest = new XMLHttpRequest();
		let url: string = config.url;
		let params: string = "";

		//提交组合参数
		for (let prop in config.params) {
			params += "&" + prop + "=" + config.params[prop];
		}
		params = params.replace(/^&/, "");

		//非POST方式提交配置
		if (config.method !== "POST") {
			url += `?${params}`;
			config.method = "GET";
		}

		xmlhttp.addEventListener("readystatechange", () => {
			if (xmlhttp.readyState === 4) {
				if (xmlhttp.status === 200) {
					callback ? callback(JSON.parse(xmlhttp.responseText)) : "";
				} else {
					console.log("服务器响应状态：", xmlhttp.status);
				}
			} else {
				console.log("请求结果：", xmlhttp.readyState);
			}
		});
		xmlhttp.open(config.method, url, true);

		//POST方式提交发送数据
		if (config.method === "POST") {
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.send(params);
		}

		//GET方式提交发送数据
		else {
			xmlhttp.send();
		}
		
	}


	/********************************* 短信验证请求 ***********************************/

	static smsCheck(config: {
		url: string,
		params: Object
	}, callback?: Function): void {
		let tokenkey: string	= "lkasdj213489ulkjadsf";
		let requestData: string	= JSON.stringify(config.params);
		let key: string			= md5(requestData + tokenkey);
		Utils.ajax({
			url: config.url,
			params: {"data": requestData, "key": key},
			method: "POST"
		}, callback);
	}


	/********************************* 格式化时间 ***********************************/

	static formatTime(format: string, millisecond: number): string {
		let reg_hh = /HH+/g;
		let reg_h = /H/g;
		let reg_mm = /mm+/g;
		let reg_m = /m/g;
		let reg_ss = /ss+/g;
		let reg_s = /s/g;
		let reg_SSS = /SSS+/g;
		let reg_SS = /SS/g;
		let reg_S = /S/g;

		//传入格式最大单位为小时
		if (reg_hh.test(format) || reg_h.test(format)) {
			let h = Math.floor(millisecond / 360000);
			let m = Math.floor(millisecond % 360000 / 60000);
			let s = Math.floor(millisecond % 360000 % 60000 / 1000);
			let S = Math.floor(millisecond % 360000 % 60000 % 1000);
			return format.replace(reg_hh, h >= 10 ? h.toString() : "0" + h)
				.replace(reg_h, h.toString())
				.replace(reg_mm, m >= 10 ? m.toString() : "0" + m)
				.replace(reg_m, m.toString())
				.replace(reg_ss, s >= 10 ? s.toString() : "0" + s)
				.replace(reg_s, s.toString())
				.replace(reg_SSS, S >= 100 ? S.toString() : (S >= 10 ? "0" + S : "00" + S))
				.replace(reg_SS, S >= 100 ? (Math.floor(S / 10)).toString() : "0" + Math.floor(S / 10))
				.replace(reg_S, (Math.floor(S / 100)).toString());
		} 
		
		//传入格式最大单位为分钟
		else if (reg_mm.test(format) || reg_m.test(format)) {
			let m = Math.floor(millisecond / 60000);
			let s = Math.floor(millisecond % 60000 / 1000);
			let S = Math.floor(millisecond % 60000 % 1000);
			return format.replace(reg_mm, m >= 10 ? m.toString() : "0" + m)
				.replace(reg_m, m.toString())
				.replace(reg_ss, s >= 10 ? s.toString() : "0" + s)
				.replace(reg_s, s.toString())
				.replace(reg_SSS, S >= 100 ? S.toString() : (S >= 10 ? "0" + S : "00" + S))
				.replace(reg_SS, S >= 100 ? (Math.floor(S / 10)).toString() : "0" + Math.floor(S / 10))
				.replace(reg_S, (Math.floor(S / 100)).toString());
		} 
		
		//传入格式最大单位为秒
		else if (reg_ss.test(format) || reg_s.test(format)) {
			let s = Math.floor(millisecond / 1000);
			let S = Math.floor(millisecond % 1000);
			return format.replace(reg_ss, s >= 10 ? s.toString() : "0" + s)
				.replace(reg_s, s.toString())
				.replace(reg_SSS, S >= 100 ? S.toString() : (S >= 10 ? "0" + S : "00" + S))
				.replace(reg_SS, S >= 100 ? (Math.floor(S / 10)).toString() : "0" + Math.floor(S / 10))
				.replace(reg_S, (Math.floor(S / 100)).toString());
		} 
		
		//传入格式最大单位为毫秒
		else {
			let S = Math.floor(millisecond);
			return format.replace(reg_SSS, S >= 100 ? S.toString() : (S >= 10 ? "0" + S : "00" + S))
				.replace(reg_SS, S >= 10 ? S.toString() : "0" + S)
				.replace(reg_S, S.toString());
		}
	}
}